<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/3/15 1:07
 * @version
 */
namespace Modules\ApiV2\Controllers;

use App\Models\AppControl;
use Illuminate\Http\Request;

class AppController extends BaseController
{
    //APP更新
    public function control(Request $request)
    {
        $platform = $request->header("app-platform");
        $version = $request->header("app-version");
        if (!in_array($platform,['android','ios'])){
            return $this->fail("'app-platform'超出取值范围");
        }
        $info = (new AppControl())->where("app_name",$platform)->orderBy('id','desc')->first();
        if (!$info){
            return $this->fail("未找到版本更新记录");
        }
        $fuHao = compareAppVersion($info->update_to_version,$version);
        if ($fuHao=='lt' || $fuHao=='eq') {
            return ['need_update' => false];
        }
        $data = ['need_update' => false, 'force_update' => false];
        $data['open_weixin_login'] = $info['open_weixin_login']?true:false;
        $data['download_url'] = $info['download_url'];
        $data['update_words'] = $info['update_words'];
        if ($info['is_update'] || $info['force_update']){
            $data['need_update'] = $info['is_update']?true:false;
            $data['force_update'] = $info['force_update']?true:false;
        }
        return success($data);
    }
}